using System;
using System.Data;
using System.Data.OracleClient;

namespace Tort.Utility.Conversion
{
    /// <summary>
    /// Utility class to convert native types to respective db types.
    /// </summary>
    internal class DbTypeConverter
    {
        internal DbTypeConverter() { }

        //private Type type;
        //private DbType dbType;
        private SqlDbType sqlDbType;
        private OracleType oracleType;
        private DbTypeMapping mapping;

        //private bool IgnoreUnicode { get; set; }

        /// <summary>
        /// Converts a native type to generic DBType. Only to be used if the real DB engine is not known.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        /// <exception cref="PersistenceException"></exception>
        //internal DbType ConvertToDbType(object obj)
        //{
        //    Convert(obj.GetType());
        //    if (!mapping.DbTypeSet)
        //    {
        //        throw new TortException.PersistenceException(obj.GetType().Name + "not supported!");
        //    }
        //    return dbType;
        //}

        /// <summary>
        /// Converts a native type to a Sql Server Db Type
        /// </summary>
        /// <param name="typeToConvert"></param>
        /// <returns></returns>
        /// <exception cref="PersistenceException"></exception>
        internal SqlDbType ConvertToSqlDbType(Type typeToConvert)
        {
            Convert(typeToConvert);
            if (!mapping.SqlDbTypeSet)
            {
                throw new TortException.PersistenceException(typeToConvert.Name + " not supported!");
            }
            return sqlDbType;
        }

        /// <summary>
        /// Converts a native type to a Oracle Db Type
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        /// <exception cref="PersistenceException"></exception>
        internal OracleType ConvertToOracleDbType(Type typeToConvert)
        {
            Convert(typeToConvert);
            if (!mapping.OracleDbTypeSet)
            {
                throw new TortException.PersistenceException(typeToConvert.Name + " not supported!");
            }
            return oracleType;
        }

        //internal Type ConvertToNativeType(SqlDbType typeToConvert)
        //{
        //    Convert(typeToConvert);
        //    if (!mapping.NativeTypeSet)
        //    {
        //        throw new TortException.PersistenceException(sqlDbType.ToString() + " not supported!");
        //    }
        //    return type;
        //}

        private void Convert(Type typeToConvert)
        {
            foreach (DbTypeMapping dbTypeMapping in DbTypeMappingCollection.DbTypeMappings(Database.Providers.DbDetails.IgnoreUnicode))
            {
                if (typeToConvert == dbTypeMapping.NativeType)
                {
                    if (dbTypeMapping.DbTypeSet)
                    {
                        //dbType = dbTypeMapping.DbType;
                        this.mapping = dbTypeMapping;
                    }
                    if (dbTypeMapping.SqlDbTypeSet)
                    {
                        sqlDbType = dbTypeMapping.SqlDbType;
                        this.mapping = dbTypeMapping;
                    }
                    if (dbTypeMapping.DbTypeSet)
                    {
                        oracleType = dbTypeMapping.OracleType;
                        this.mapping = dbTypeMapping;
                    }
                }
            }
        }

        //private void Convert(SqlDbType typeToConvert)
        //{
        //    foreach (DbTypeMapping dbTypeMapping in DbTypeMappingCollection.DbTypeMappings(Database.Providers.DbDetails.IgnoreUnicode))
        //    {
        //        if (dbTypeMapping.SqlDbType == typeToConvert)
        //        {
        //            type = dbTypeMapping.NativeType;
        //            this.mapping = dbTypeMapping;
        //        }
        //    }
        //}
    }
}
